package com.zkca.xf.ui.activity.technicalprotect;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.FireAlarmBean;
import com.zkca.xf.model.javabean.SmokeBean;
import com.zkca.xf.presenter.SmokePresenter;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.view.SmokeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

//烟感、手报
public class SmokeActivity extends BaseTitleActivity implements SmokeView {

    @BindView(R.id.xrv_fa)
    XRecyclerView xrv_fa;
    private List<SmokeBean.DataBean> dataList;
    private String type;

    private  String user_id;
    private  Map<String, String> map;
    private int numble=0;
    private SingleAdapter<SmokeBean.DataBean> adapter;
    private LinearLayoutManager linearLayoutManager;
    private SmokePresenter smokePresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smoke);
        ButterKnife.bind(this);
        type= getIntent().getStringExtra("type");
        smokePresenter=new SmokePresenter(this,this);
        initview();
        initdata();
    }

    private void initdata() {

        user_id = SPUtil.getString(this,Constants_SP.USER_ID, "");
        map= new HashMap();
        map.put("userId", user_id);
        map.put("page", numble+"");
        map.put("size", "30");
        map.put("s5", "1");
        if (type.equals("shui")) {
            map.put("columnId", "1118");     //   405 故障电弧     972 独立烟感    1118 消防水
        }else if(type.equals("yangan")){
            map.put("columnId", "972");
        } else {
            map.put("columnId", "405");
        }

        smokePresenter.smoke(map,type);
    }

    private void initview() {
        dataList=new ArrayList<>();
        //   405 故障电弧     972 独立烟感    1118 消防水
        if (type.equals("shui")) {
            setTitleText("消防水检测");
        }else if(type.equals("yangan")){
            setTitleText("无线火灾");
        } else {
            setTitleText("故障电弧");
        }
        setleftTextVisibility(true);
        setleftImgVisibility(true);



        linearLayoutManager = new LinearLayoutManager(mContext);
        xrv_fa.setLayoutManager(linearLayoutManager);
        xrv_fa.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));


        xrv_fa.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                dataList.clear();
                numble=0;
                map= new HashMap<>();
                map.put("userId", user_id);
                map.put("page", numble+"");
                map.put("size", "30");
                map.put("s5", "1");
                if (type.equals("shui")) {
                    map.put("columnId", "1118");     //   405 故障电弧     972 独立烟感    1118 消防水
                }else if(type.equals("yangan")){
                    map.put("columnId", "972");
                } else {
                    map.put("columnId", "405");
                }
                smokePresenter.smoke(map,type);
            }

            @Override
            public void onLoadMore() {
                numble++;
                map= new HashMap<>();
                map.put("userId", user_id);
                map.put("page", numble+"");
                map.put("size", "30");
                map.put("s5", "1");
                if (type.equals("shui")) {
                    map.put("columnId", "1118");     //   405 故障电弧     972 独立烟感    1118 消防水
                }else if(type.equals("yangan")){
                    map.put("columnId", "972");
                } else {
                    map.put("columnId", "405");
                }
                smokePresenter.smoke(map,type);
            }
        });


        adapter = new SingleAdapter<SmokeBean.DataBean>(mContext, dataList, R.layout.xiaowei_item) {
            @Override
            public void convert(ViewHolder holder, SmokeBean.DataBean dataBean, int position) {
                TextView yhname = holder.getView(R.id.yhname);
                TextView zdadss = holder.getView(R.id.zdadss);
                TextView jjtime = holder.getView(R.id.jjtime);
                TextView zdname = holder.getView(R.id.zdname);
                TextView jqleixing = holder.getView(R.id.jqleixing);
                TextView message = holder.getView(R.id.message);
                TextView jqnum = holder.getView(R.id.jqnum);

                String gongshi = dataBean.getGongshi();
                if (gongshi.indexOf("::") != -1) {
                    String[] split1 = gongshi.split("::");
                    String yonghumingcheng = split1[0];
                    String weizhi = split1[1];
//                    yhname.setText("" + yonghumingcheng);
                    zdadss.setText("终端位置：" + weizhi);
                }
                yhname.setText("" + dataBean.getOrgName());

                jjtime.setText("接警时间：" + dataBean.getCreateTime());

                zdname.setText("终端名称：" + dataBean.getDeviceName());

                String micInfo = dataBean.getMicInfo();
                if (type.equals("shui")) {
                    try {
                       jqleixing.setText("警情描述：" + micInfo);
                       message.setText("消防水");
                    } catch (Exception e) {
                    }
                } else if (type.equals("yangan")){
                    try {
                        String[] split = micInfo.split("#");
                        String jingnum = split[0];
                        String bjleixing = split[1];
                        jqleixing.setText("警情类型：" + bjleixing);
                        jqnum.setText("警情号码：" + jingnum);
                        message.setText("烟感");
                    } catch (Exception e) {

                    }
                }else{
                    try {
                        String[] split = micInfo.split("#");
                        String jingnum = split[0];
                        String bjleixing = split[1];
                        String jqmiaoshu = split[2];
                        jqleixing.setText("警情类型：" + bjleixing);
                        jqnum.setText("警情号码：" + jingnum);
                        message.setText("" + jqmiaoshu);
                    } catch (Exception e) {

                    }

                }
            }
        };
        xrv_fa.setAdapter(adapter);
    }


    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getSmoke(SmokeBean s) {
        try{
            xrv_fa.refreshComplete();
            xrv_fa.loadMoreComplete();
            if(  s.getStatus()==1){
                List<SmokeBean.DataBean> data =  s.getData();
                if (data != null && data.size() >0) {
                    dataList.addAll(data);
                }
                if (data.size()<10){
                    xrv_fa.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
        }
    }

    @Override
    public void getArc(SmokeBean s) {
        try{
            xrv_fa.refreshComplete();
            xrv_fa.loadMoreComplete();
            if(  s.getStatus()==1){
                List<SmokeBean.DataBean> data =  s.getData();
                if (data != null && data.size() >0) {
                    dataList.addAll(data);
                }
                if (data.size()<10){
                    xrv_fa.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
        }
    }

    @Override
    public void getWater(SmokeBean s) {
        try{
            xrv_fa.refreshComplete();
            xrv_fa.loadMoreComplete();
            if(  s.getStatus()==1){
                List<SmokeBean.DataBean> data =  s.getData();
                if (data != null && data.size() >0) {
                    dataList.addAll(data);
                }
                if (data.size()<10){
                    xrv_fa.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
        }
    }
}
